package com.example.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Slf4j
public class DatabaseConfig {
    private static final Properties properties = new Properties();

    static {
        try {
            properties.load(DatabaseConfig.class.getClassLoader()
                    .getResourceAsStream("application.properties"));
        } catch (IOException e) {
            log.error("Failed to load application.properties", e);
        }
    }

    public static DataSource createDataSource(String schema) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(properties.getProperty("db.url") + schema);
        config.setUsername(properties.getProperty("db.username"));
        config.setPassword(properties.getProperty("db.password"));
        config.setMaximumPoolSize(Integer.parseInt(properties.getProperty("db.maxPoolSize", "10")));

        return new HikariDataSource(config);
    }
}
